package cn.ds.assets.module.assets.controller.admin.metadataModelBase;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.metadataModelBase.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelBase.BaseMetadataModelBaseDO;
import cn.ds.assets.module.assets.convert.metadataModelBase.BaseMetadataModelBaseConvert;
import cn.ds.assets.module.assets.service.metadataModelBase.BaseMetadataModelBaseService;

//@Tag(name = "管理后台 - 元数据管理-元模型-基础属性")
//@RestController
//@RequestMapping("/assets/base-metadata-model-base")
@Validated
public class BaseMetadataModelBaseController {

    @Resource
    private BaseMetadataModelBaseService baseMetadataModelBaseService;

    @PostMapping("/create")
    @Operation(summary = "创建元数据管理-元模型-基础属性")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:create')")
    public CommonResult<Long> createBaseMetadataModelBase(@Valid @RequestBody BaseMetadataModelBaseCreateReqVO createReqVO) {
        return success(baseMetadataModelBaseService.createBaseMetadataModelBase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新元数据管理-元模型-基础属性")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:update')")
    public CommonResult<Boolean> updateBaseMetadataModelBase(@Valid @RequestBody BaseMetadataModelBaseUpdateReqVO updateReqVO) {
        baseMetadataModelBaseService.updateBaseMetadataModelBase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除元数据管理-元模型-基础属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:delete')")
    public CommonResult<Boolean> deleteBaseMetadataModelBase(@RequestParam("id") Long id) {
        baseMetadataModelBaseService.deleteBaseMetadataModelBase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得元数据管理-元模型-基础属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:query')")
    public CommonResult<BaseMetadataModelBaseRespVO> getBaseMetadataModelBase(@RequestParam("id") Long id) {
        BaseMetadataModelBaseDO baseMetadataModelBase = baseMetadataModelBaseService.getBaseMetadataModelBase(id);
        return success(BaseMetadataModelBaseConvert.INSTANCE.convert(baseMetadataModelBase));
    }

    @GetMapping("/list")
    @Operation(summary = "获得元数据管理-元模型-基础属性列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:query')")
    public CommonResult<List<BaseMetadataModelBaseRespVO>> getBaseMetadataModelBaseList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseMetadataModelBaseDO> list = baseMetadataModelBaseService.getBaseMetadataModelBaseList(ids);
        return success(BaseMetadataModelBaseConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得元数据管理-元模型-基础属性分页")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:query')")
    public CommonResult<PageResult<BaseMetadataModelBaseRespVO>> getBaseMetadataModelBasePage(@Valid BaseMetadataModelBasePageReqVO pageVO) {
        PageResult<BaseMetadataModelBaseDO> pageResult = baseMetadataModelBaseService.getBaseMetadataModelBasePage(pageVO);
        return success(BaseMetadataModelBaseConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出元数据管理-元模型-基础属性 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-model-base:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseMetadataModelBaseExcel(@Valid BaseMetadataModelBaseExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseMetadataModelBaseDO> list = baseMetadataModelBaseService.getBaseMetadataModelBaseList(exportReqVO);
        // 导出 Excel
        List<BaseMetadataModelBaseExcelVO> datas = BaseMetadataModelBaseConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "元数据管理-元模型-基础属性.xls", "数据", BaseMetadataModelBaseExcelVO.class, datas);
    }

}
